package com.fengxu.springcloud.service.impl;

import com.fengxu.springcloud.dao.OrderDao;
import com.fengxu.springcloud.doman.Order;
import com.fengxu.springcloud.service.AccountService;
import com.fengxu.springcloud.service.OrderService;
import com.fengxu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AccountService accountService;

    @Autowired
    private StorageService storageService;

    @GlobalTransactional(name = "唯一的就行",rollbackFor = Exception.class)
    @Override
    public void create(Order order) {
        log.info("----->开始创建订单");
        orderDao.create(order);

        log.info("------>订单微服务开始调用库存，做扣减");
        storageService.decrease(order.getProductId(),order.getCount());

        log.info("----->用户账户扣除相应金额");
        accountService.decrease(order.getUserId(), order.getMoney());

        log.info("-------->将订单状态修改至完成！");
        this.update(order.getId(), 1);

        log.info("--------->下单结束！");

    }

    @Override
    public void update(Long id, Integer status) {
        orderDao.update(id,status);
    }
}
